﻿using System;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 系统登录日志
    /// </summary>
    public partial class LoginLogManagerUserControl : BaseUserControl
    {
        public LoginLogManagerUserControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载首页数据
        /// </summary>
        public Task LoadFirstPageAsync()
        {
            OnceAction.Run("init", () =>
            {
                if (!WinFormHelper.IsDesignMode())
                {
                    gridControl.ModelType = typeof(SysLoginLog);
                    editOperateStartDateTime.ConfigDate();
                    editOperateEndDateTime.ConfigDate();
                    Mutex.Begin(() => App.ContainerData.SetModel(layoutGroupFilter, SysLoginLogFilter.Default));
                }
            });
            return Mutex.EndAsync(() => gridControl.LoadFirstPageAsync());
        }

        /// <summary>
        /// 搜索事件
        /// </summary>
        private async void btnSearch_Click(object sender, EventArgs e)
        {
            await LoadFirstPageAsync();
        }

        /// <summary>
        /// 重置事件
        /// </summary>
        private async void btnReset_Click(object sender, EventArgs e)
        {
            Mutex.Begin(() => App.ContainerData.SetModel(layoutGroupFilter, SysLoginLogFilter.Default));
            await LoadFirstPageAsync();
        }

        private async void editControl_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == R.EnterKey) await LoadFirstPageAsync();
        }

        private async void editStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            await LoadFirstPageAsync();
        }

        private void GridControl_DetectFilter(object sender, DetectFilterEventArgs e)
        {
            e.Filter = App.ContainerData.GetModel(layoutGroupFilter, new SysLoginLogFilter { });
        }

        private void GridControl_LoadData(object sender, LoadDataEventArgs e)
        {
            e.Task = async () => await SysLogService.Instance.SelectLoginLogPageListAsync(e.Filter as SysLoginLogFilter);
        }

        private void GridControl_RowDoubleClick(object sender, EventArgs e)
        {
            new FrmLoginLogDetails(gridControl).ShowDialogOk();
        }
    }
}
